<template>
  <basePage :show-tabbar="false" :page-params="pageParams">
    <view class="title">{{ title }}</view>
    <view class="money">
      <view style="font-size: 24px;">￥</view>
      <text>{{ Number(orderInfo.amount).toFixed(2) }}</text>
    </view>
    <view class="pay-button" @click.stop="$u.throttle(paymentFn, 500)">立即支付</view>
  </basePage>
</template>

<script>
import { getAuthCode } from '@/utils/login';
import basePage from '@/components/base-page';
import { YBPayment } from '@/api/payment';
export default {
  name: 'AppOrderPayment',
  components: {
    basePage
  },
  data() {
    return {
      title: '鑫方盛商品',
      orderInfo: {}, //订单信息
      pageParams: {
        state: null,
        emptyTitle: '',
        errorTitle: '',
        showButton: false
      }
    }
  },
  onLoad(option) {
    this.orderInfo = option
  },
  methods: {
    paymentFn() {
      const provider = this.vuex_provider;
      getAuthCode(provider, (loginRes) => {
        if (!loginRes.code) {
          this.$u.toast('获取code失败')
          return
        }
        this.YBPaymentFn(loginRes.code)
      })
    },
    YBPaymentFn(code) {
      const {
        id,
        amount,
        ip,
        serialno,
      } = this.orderInfo
      const params = {
        params: [{
          'platform': '20',
          'payWayName': 'YeeWechatPay',
          'payId': id,
          'paySerialnoId': serialno,
          'payableAmount': amount,
          'ipAddress': ip,
          'wxCode': code,
        }]
      }
      YBPayment(params).then((res) => {
        if (res.errorCode != 0) {
          this.$u.toast(res.msg)
          return
        }
        this.requestPaymentFn(res.data.prePayTn, amount)
      }).catch((res) => {
        this.pageParams.state = 'ERROR'
        this.pageParams.errorTitle = res.errMsg
      })
    },
    /*调起支付*/
    requestPaymentFn(prePayTn, amount) {
      // 调用微信支付
      // #ifdef MP-WEIXIN
      wx.requestPayment({
        timeStamp: prePayTn.timeStamp,
        nonceStr: prePayTn.nonceStr,
        package: prePayTn.package,
        paySign: prePayTn.paySign,
        signType: prePayTn.signType,
        success(res) {
          // 支付成功跳转页面
          wx.redirectTo({
            url: '/payments/app-payment-success/app-payment-success?amount=' + amount
          })
        },
        fail(err) {
          uni.$u.toast(err.errMsg)
        }
      })
      // #endif
    },
  }
}
</script>

<style scoped lang="scss">
.title {
  padding-top: 20rpx;
  font-size: 32rpx;
  font-weight: 600;
  justify-content: center;
  display: flex;
}

.money {
  margin-top: 20rpx;
  font-size: 64rpx;
  justify-content: center;
  flex-direction: row;
  display: flex;
}

.pay-button {
  margin: 60rpx 40rpx 0;
  justify-content: center;
  align-items: center;
  height: 88rpx;
  background-color:$uni-orange;
  color: white;
  display: flex;
  border-radius: 44rpx;
  font-size: 32rpx;
  font-weight: 500;
}
</style>
